package com.bianguo.android.iocdemo.view.widget.parallax;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.bianguo.android.iocdemo.R;
import com.bianguo.android.iocdemo.view.fragment.ParallaxFragment;

public class ParallaxInflater extends LayoutInflater {

    private ParallaxFragment fragment;

    protected ParallaxInflater(Context context) {
        super(context);
    }

    protected ParallaxInflater(LayoutInflater original, Context newContext) {
        super(original, newContext);
    }

    public ParallaxInflater(LayoutInflater original, Context newContext, ParallaxFragment fragment) {
        super(original, newContext);
        this.fragment = fragment;
        //对xml文件转View的过程的监听
        setFactory2(new ParallaxFactory(this));
    }

    @Override
    public LayoutInflater cloneInContext(Context newContext) {
        return new ParallaxInflater(this, newContext, fragment);
    }

    class ParallaxFactory implements Factory2 {

        private LayoutInflater inflater;
        private final String[] sClassPrefix = {
                "android.widget.",
                "android.view."
        };
        private final int[] attrIds = {
                R.attr.a_in,
                R.attr.a_out,
                R.attr.x_in,
                R.attr.x_out,
                R.attr.y_in,
                R.attr.y_out
        };

        public ParallaxFactory(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        @Override
        public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
            View view = null;
            view = createMyView(name, context, attrs);
            if (view != null) {
                TypedArray typedArray = context.obtainStyledAttributes(attrs, attrIds);
                if (typedArray != null && typedArray.length() > 0) {
                    ParallaxViewTag tag = new ParallaxViewTag();
                    tag.setaIn(typedArray.getFloat(0, 0f));
                    tag.setaOut(typedArray.getFloat(1, 0f));
                    tag.setxIn(typedArray.getFloat(2, 0f));
                    tag.setxOut(typedArray.getFloat(3, 0f));
                    tag.setyIn(typedArray.getFloat(4, 0f));
                    tag.setyOut(typedArray.getFloat(5, 0f));
                    view.setTag(R.id.parallax_tag, tag);
                }
                fragment.getParallaxView().add(view);
                typedArray.recycle();
            }
            return view;
        }

        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {
            return null;
        }

        /**
         * 创建View
         * @param name xml文件中的标签比如 ImageView, TextView, LinearLayout.....
         * @param context 上下文
         * @param attrs xml文件中的属性比如 layout_width, a_in, x_out .....
         * @return
         */
        private View createMyView(String name, Context context, AttributeSet attrs) {
            if (name.contains(".")) {
                return reflectView(name, null, context, attrs);
            } else {
                for (String classPrefix : sClassPrefix) {
                    View view = reflectView(name, classPrefix, context, attrs);
                    if (view != null) {
                        return view;
                    }
                }
            }
            return null;
        }

        /**
         * 创建View
         * @param name xml文件中的标签比如 ImageView, TextView, LinearLayout.....
         * @param prefix 上一个name的前缀比如 android.widget., android.view., 如果是自定义View则不需要
         * @param context
         * @param attrs xml文件中的属性比如 layout_width, a_in, x_out .....
         * @return
         */
        private View reflectView(String name, String prefix, Context context, AttributeSet attrs) {
            try {
                return inflater.createView(name, prefix, attrs);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
